using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class EquipButton : MonoBehaviour
{
    private int _index;
    private Action<int> _action;

    private Transform _select;
    private Image _equip;
    private TextMeshProUGUI _text;
    private Transform _lock;

    public void Init(int index, Action<int> action)
    {
        _index = index;

        _select = transform.Find("Select");
        _equip = transform.Find("Equip").GetComponent<Image>();
        _text = transform.Find("Text").GetComponent<TextMeshProUGUI>();
        _lock = transform.Find("Lock");
        Sprite sp = GameManager.Instance.LoadSprite($"{Constants.PathWaterGunSprite}_{index}", true);
        _equip.sprite = sp;
        _action = action;

        GetComponent<Button>().onClick.AddListener(OnClick);
    }

    public void Refresh(int index)
    {
        EquipData data = GameManager.Instance._equipDatas[_index];
        _lock.gameObject.SetActive(!data.IsUnlock);
        if (data.IsUnlock)
        {
            _text.text = index == GameManager.Instance.WaterGunIndex ? "已装备" : "";
        }
        else
        {
            _text.text = $"￥{GameManager.Instance._equipDatas[_index].EquipCost}";
        }
    }

    void OnClick()
    {
        _action?.Invoke(_index);
    }

    public void SetSelect(bool isShow)
    {
        _select.gameObject.SetActive(isShow);
    }

    public void ShowBtnAnim(bool isShow)
    {
        _select.gameObject.SetActive(isShow);
        _equip.transform.DOKill();
        _equip.transform.localRotation = Quaternion.Euler(0.0f, 0.0f, 0.0f);

        if (isShow)
        {
            _select.transform.localScale = Vector3.zero;
            _select.transform.DOScale(Vector3.one, 0.1f).SetEase(Ease.InCubic);
            _equip.transform.DOShakeRotation(1f, new Vector3(10, 10, 10), 10, 180, false)
                .SetLoops(-1, LoopType.Incremental);
        }
    }
}